//
//  TZJLViewController.m
//  NewPro
//
//  Created by RYX on 2019/4/24.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "TZJLViewController.h"
#import "TodayEarningsStore.h"
#import "IncomeListModel.h"
#import "TZJLCell.h"
#import "TodayEarningsList.h"

@interface TZJLViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger current;
    NSInteger size;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic, strong) NSMutableArray *listArr;

@end

@implementation TZJLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        
    self.listArr = [NSMutableArray array];
    
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(5);
    }];
    
    current = 1;
    size = 100;
    
    [self loadData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"TODAYEARNING" object:nil];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)refresh{
    
    current = 1;
    [self loadData];
}
- (void)loadData{
    
    NSString *cyear = [NSString stringWithFormat:@"%ld",(long)[TodayEarningsList sharedInstance].year];
    NSString *cmonth = [NSString stringWithFormat:@"%ld",(long)[TodayEarningsList sharedInstance].month];
    NSString *cday = [NSString stringWithFormat:@"%ld",(long)[TodayEarningsList sharedInstance].day];
    NSString *ccurrent = [NSString stringWithFormat:@"%ld",(long)current];
    NSString *csize = [NSString stringWithFormat:@"%ld",(long)size];
    
    MJWeakSelf
    TodayEarningsStore *store = [[TodayEarningsStore alloc] init];
    [store getTZJLListWithYear:cyear month:cmonth day:cday current:ccurrent size:csize Success:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_footer endRefreshing];
        if (self->current == 1) {
            
            weakSelf.listArr = [NSMutableArray arrayWithArray:listArr];
        }else{
            
            [weakSelf.listArr addObjectsFromArray:listArr];
        }
        
        if (listArr.count < 100) {
            
            weakSelf.tableView.mj_footer.hidden = YES;
        }else{
            
            weakSelf.tableView.mj_footer.hidden = NO;
        }
        
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [self.tableView.mj_footer endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] init];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.separatorColor = BASECOLOR;
        MJWeakSelf
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            self->current ++ ;
            [weakSelf loadData];
        }];
        
        _tableView.mj_footer = footer;
        _tableView.mj_footer.hidden = YES;
    }
    
    return _tableView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 53;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TZJLCell *cell = [TZJLCell tempWithTableView:tableView];
    cell.model = self.listArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.scrollCallback ?: self.scrollCallback(scrollView);
}


/**
 返回listView。如果是vc包裹的就是vc.view；如果是自定义view包裹的，就是自定义view自己。
 */
- (UIView *)listView {
    return self.view;
}

/**
 返回listView内部持有的UIScrollView或UITableView或UICollectionView
 主要用于mainTableView已经显示了header，listView的contentOffset需要重置时，内部需要访问到外部传入进来的listView内的scrollView
 */
- (UIScrollView *)listScrollView {
    return self.tableView;
}


/**
 当listView内部持有的UIScrollView或UITableView或UICollectionView的代理方法`scrollViewDidScroll`回调时，需要调用该代理方法传入的callback
 */
- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}
@end
